<?php
namespace app\common\component\user_wallet_log;

use think\Session;

use app\common\component\user_wallet\UserWalletService;

class UserWalletLogService {

	public function getUserWalletLogs($type = null, $page = 10) {
		$userWalletLogModel = (new UserWalletLogModel()) -> where('to_user_name', Session::get('user_name'));
		if(!is_null($type)) {
			$userWalletLogModel -> where('type', $type);
		}
		return $userWalletLogModel -> order('is_state ASC, user_wallet_log_id DESC') -> paginate($page, true);
	}
	
	public function getAll($page = 10) {
		return UserWalletLogModel::order('is_state ASC, user_wallet_log_id DESC') -> paginate($page);
	}

	public function create($data) {
		$userWalletLogModel = new UserWalletLogModel($data);

    	if($userWalletLogModel -> save()) {
    		return [
    			'code' => 0,
    			'message' => '日志成功!',
    			'data' => [
    				'user_wallet_log' => $userWalletLogModel
    			]
    		];
    	}

    	return [
			'code' => -1,
			'message' => '日志失败!'
		];
	}

	/*
	 * @value type 记录属性,1.充值,2.开通/续费会员,3.提现,4.本金转入,5.利息转入,6.奖励转入
	 * @value active 金额加或减操作,0.减,1.加
	 * @value price 金额
	 * @value $to_user_name 充值到哪个用户
	 * @value remark 备注
	 * @value is_state 金额是否已经领取,0.否,1.是
	 * @value from_user_name 充值从哪个用户来
	 * @value voucher 凭证图片
	 */
	public function log($type, $active, $price, $to_user_name, $remark, $is_state, $from_user_name = null, $voucher = '') {
		$resultUser = \app\common\component\user\UserModel::get(['username' => $to_user_name]);

		if($resultUser) {
			$post = array();
			$post['type'] = $type;
			$post['active'] = $active;
			$post['price'] = $price;
			if($from_user_name) {
				$post['from_user_name'] = $from_user_name;
			} else {
				$post['from_user_name'] = \think\Session::get('user_name');
			}
			$post['to_user_name'] = $resultUser['username'];
			$resultUserWallet = (new UserWalletService) -> getUserWallet($resultUser['user_id']);
			$post['to_user_name_old_money'] = $resultUserWallet['data']['user_wallet'] -> money;
			if($active == 1) {
				$post['to_user_name_new_money'] = $resultUserWallet['data']['user_wallet'] -> money + $post['price'];
			} else {
				$post['to_user_name_new_money'] = $resultUserWallet['data']['user_wallet'] -> money - $post['price'];
			}
			$post['create_time'] = time();
			$post['voucher'] = $voucher;
			$post['remark'] = $remark;
			$post['is_state'] = $is_state;

	    	return $this -> create($post);
		}
		
		return [
			'code' => -1,
			'message' => '用户不存在'
		];
	}

	public function logUserId($type, $active, $price, $to_user_id, $remark, $is_state, $from_user_name = null, $voucher = '') {
		$resultUser = \app\common\component\user\UserModel::get(['user_id' => $to_user_id]);

		if($resultUser) {
			$post = array();
			$post['type'] = $type;
			$post['active'] = $active;
			$post['price'] = $price;
			if($from_user_name) {
				$post['from_user_name'] = $from_user_name;
			} else {
				$post['from_user_name'] = $resultUser['username'];
			}
			$post['to_user_name'] = $resultUser['username'];
			$resultUserWallet = (new UserWalletService) -> getUserWallet($resultUser['user_id']);
			$post['to_user_name_old_money'] = $resultUserWallet['data']['user_wallet'] -> money;
			if($active == 1) {
				$post['to_user_name_new_money'] = $resultUserWallet['data']['user_wallet'] -> money + $post['price'];
			} else {
				$post['to_user_name_new_money'] = $resultUserWallet['data']['user_wallet'] -> money - $post['price'];
			}
			$post['create_time'] = time();
			$post['voucher'] = $voucher;
			$post['remark'] = $remark;
			$post['is_state'] = $is_state;

	    	return $this -> create($post);
		}
		return [
			'code' => -1,
			'message' => '用户不存在'
		];
	}
}
?>